{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1feb22b1",
   "metadata": {},
   "source": [
    "## 3.1列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "78ea3a33",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['Tom', 'Bob', 'Tim'], ['Paris', 'HongKong', 'California'], 'Class1 ']\n",
      "['Tom', 'Bob', 'Tim', 'Paris', 'HongKong', 'California']\n",
      "California\n"
     ]
    }
   ],
   "source": [
    "#列表长度用len()输出一个整数\n",
    "\n",
    "#列表在定义之后可以嵌套\n",
    "name=[\"Tom\",\"Bob\",\"Tim\"]\n",
    "places=[\"Paris\",\"HongKong\",\"California\"]\n",
    "message=[name,places,\"Class1 \"]\n",
    "comb=name+places\n",
    "\n",
    "print(message)\n",
    "print(comb)#直接相加是把列表按照先后顺序直接合并\n",
    "print(message[1][2])#嵌套之后可以两层调用"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce3455b4",
   "metadata": {},
   "source": [
    "### Slice切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f642275b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Pepperoni', 'Margarita']\n",
      "['Chicago Style', 'California Style', 'New York Style']\n",
      "['Pepperoni', 'Chicago Style', 'New York Style']\n"
     ]
    }
   ],
   "source": [
    "pizzas=[\"Pepperoni\",\"Margarita\",\"Chicago Style\",\"California Style\",\"New York Style\"]\n",
    "print(pizzas[:-3])#输出到倒数第三个\n",
    "print(pizzas[-3:])#输出后三个\n",
    "print(pizzas[::2])#实现间隔输出，最后一个数是步长"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "656d8a2a",
   "metadata": {},
   "source": [
    "### 列表的函数\n",
    "\n",
    "+ max和min函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "ba7010ae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "27\n",
      "2\n",
      "Pepperoni\n",
      "California Style\n"
     ]
    }
   ],
   "source": [
    "#max min 输出最值,如果是字符则输出ASCII最大或最小的字符\n",
    "num=list(range(2,32,5))\n",
    "print(max(num))\n",
    "print(min(num))\n",
    "\n",
    "#比较ASCII的时候只考虑第一个字符的码值，均相同再考虑第二个\n",
    "print(max(pizzas))\n",
    "print(min(pizzas))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "b18aac50",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#检查元素，要保证大小写完全一致\n",
    "\"pepperoni\"in pizzas"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "327d560f",
   "metadata": {},
   "source": [
    "#### 修改列表元素、查找元素位置的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4c45770c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Tom', 'Bob', 'Mary', 'Tim', 'Jim']\n",
      "['Tom', 'Bob', 'Mary', 'Tim', 'Jim', ['Pepperoni', 'Margarita']]\n",
      "['Tom', 'Bob', 'Mary', 'Tim', 'Jim', ['Pepperoni', 'Margarita'], 'Pepperoni', 'Margarita']\n",
      "['Bob', 'Mary', 'Mary', 'Tim']\n",
      "Jim won't come tonight.\n"
     ]
    }
   ],
   "source": [
    "#append直接在列表后面添加元素\n",
    "#insert(num,element)在中间加入元素\n",
    "name=['Tom', 'Bob','Tim']\n",
    "pizzas=['Pepperoni', 'Margarita']\n",
    "name.append(\"Jim\")\n",
    "name.insert(2,\"Mary\")\n",
    "print(name)\n",
    "\n",
    "#如果后面加的是列表，append是直接加，extend会把列表展开，不必嵌套\n",
    "name.append(pizzas)\n",
    "print(name)\n",
    "name.extend(pizzas)\n",
    "print(name)\n",
    "\n",
    "#使用del可以删掉列表中对应的元素\n",
    "#使用remove(element)删掉列表里面第一个对应元素\n",
    "#使用pop(num)可以一并实现删除并弹出的功能，可以打印出popped word\n",
    "name=['Tom', 'Bob', 'Mary', 'Mary', 'Tim', 'Jim', 'Jim']\n",
    "del name[0]\n",
    "name.remove(\"Jim\")\n",
    "popped_name=name.pop(-1)\n",
    "print(name)\n",
    "print(popped_name+\" won't come tonight.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "eb79495e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#index用来找到某个元素的位置\n",
    "name=['Tom', 'Bob', 'Mary', 'Tim', 'Jim']\n",
    "print(name.index(\"Mary\"))#name.index()也可以直接输出\n",
    "num=list(range(2,56,5))\n",
    "num.index(32)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6eefc29a",
   "metadata": {},
   "source": [
    "+ 来组织列表顺序的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "24e0aca8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Jim', 'Tim', 'Mary', 'Bob', 'Tom']\n",
      "['Bob', 'Jim', 'Mary', 'Tim', 'Tom']\n",
      "['Tom', 'Tim', 'Mary', 'Jim', 'Bob']\n",
      "['HangZhou', 'HongKong', 'Lodon', 'New York', 'Tokyo']\n",
      "['Lodon', 'HongKong', 'Tokyo', 'HangZhou', 'New York']\n",
      "['Tokyo', 'New York', 'Lodon', 'HongKong', 'HangZhou']\n"
     ]
    }
   ],
   "source": [
    "#永久性改变(这些函数最好先单独写，然后再print)\n",
    "#reverse可以将列表逆序（相对于定义的顺序）\n",
    "name=['Tom', 'Bob', 'Mary', 'Tim', 'Jim']\n",
    "name.reverse()\n",
    "print(name)\n",
    "#sort()可以使元素按照字母顺序排列，reverse=True则按字母逆序排列\n",
    "#实际上使按照ASCII排列（如果开头有字符的话）\n",
    "name.sort()\n",
    "print(name)\n",
    "name.sort(reverse=True)#注意大写\n",
    "print(name)\n",
    "\n",
    "#临时改变，只是调用一下排列的结果，利用sorted函数\n",
    "places=[\"Lodon\",\"HongKong\",\"Tokyo\",\"HangZhou\",\"New York\"]\n",
    "print(sorted(places))\n",
    "print(places)#原来的列表并没有被改变\n",
    "print(sorted(places,reverse=True))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45769a9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#复制列表时要注意避免一些bug"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65142597",
   "metadata": {},
   "source": [
    "### 3.2元组Tuple（不能够更改元素值，使用小括号 infrequently used）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "fb6b89cd",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(['Tom', 'Tim', 'Mary', 'Jim', 'Bob'], 5)\n",
      "(['Tom', 'Tim', 'Mary', 'Jim', 'Bob', 'Lucy'], 5)\n",
      "<class 'tuple'>\n",
      "(9, 1)\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-27-f8504ae310ef>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     12\u001b[0m \u001b[0moutput\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdivmod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m28\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     13\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moutput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 14\u001b[0;31m \u001b[0moutput\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "#元组里面也可以嵌套列表，这个列表也是会保持不变的\n",
    "list=(['Tom', 'Tim', 'Mary', 'Jim', 'Bob'],5)\n",
    "print(list)\n",
    "#元组不可修改，但是里面的列表还是可以修改的\n",
    "list[0].append(\"Lucy\")\n",
    "print(list)\n",
    "print(type(list))\n",
    "\n",
    "#如果试图修改元组的值，那么就会报错\n",
    "output=divmod(28,3)\n",
    "print(output)\n",
    "output[1]=2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "31c0a934",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "#如果只有一个元素，那么默认type会是str，如果要变成元组则在后面加逗号\n",
    "dig=(\"fang\")\n",
    "print(type(dig))\n",
    "dig=(\"fang\",)\n",
    "print(type(dig))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76820ad5",
   "metadata": {},
   "outputs": [],
   "source": [
    "#元组也遵循和列表相同的切片规则"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb26e96c",
   "metadata": {},
   "source": [
    "### 3.3集合（使用的是大括号）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "272fc473",
   "metadata": {},
   "source": [
    "### 3.3.1 定义集合\n",
    "\n",
    "可以直接使用set()改变性质，也可以利用和元组类似的定义方法\n",
    "\n",
    "**集合的目的主要是消除列表里的重复元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "76f37350",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'set'>\n",
      "{1, 3, 4, 8, 9}\n"
     ]
    }
   ],
   "source": [
    "set1=set()\n",
    "print(type(set1))\n",
    "\n",
    "set2=set([1,3,4,8,9])\n",
    "print(set2)#显示的形式是大括号"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0411b489",
   "metadata": {},
   "source": [
    "### 3.3.2内置函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "bdc39cb1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set1([1,8,5,9])\n",
    "set2=([2,5,9,10])\n",
    "type(set1)\n",
    "type(set2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fbf30e47",
   "metadata": {},
   "source": [
    "**初始化是相当重要的，不然类别会识别有误\n",
    "\n",
    "**正确的定义方法如下所示："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "6cb0ae50",
   "metadata": {},
   "outputs": [],
   "source": [
    "set1=set([1,8,5,9])\n",
    "set2=set([2,5,9,10])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50193353",
   "metadata": {},
   "source": [
    "* union函数，融合两个集合，顺序排列，不重复"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1e3300bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 5, 8, 9, 10}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set1.union(set2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f5cc03b",
   "metadata": {},
   "source": [
    "* add函数可以加入一个元素,加入之后也是会将数字的序列进行整合的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "aae04116",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 5, 7, 8, 9}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set1=set([1,8,5,9])\n",
    "set1.add(7)\n",
    "set1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d633a0e2",
   "metadata": {},
   "source": [
    "* intersection输出两个集合共有元素，difference输出括号内集合不包含的元素\n",
    "* symmetric_difference输出两个集合各自独有的元素，"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "7a2b0304",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 2, 5, 8}\n",
      "set()\n",
      "{10, 12}\n",
      "{10, 12}\n"
     ]
    }
   ],
   "source": [
    "print(set1.intersection(set2))\n",
    "print(set1.difference(set2))\n",
    "\n",
    "#前后顺序对symmetric_difference没有影响\n",
    "print(set1.symmetric_difference(set2))\n",
    "print(set2.symmetric_difference(set1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e38d122",
   "metadata": {},
   "source": [
    "* issubset()用来检查是否是子集\n",
    "\n",
    "* isdisjoint()用来检查不相交\n",
    "\n",
    "* issuperset()用来检查超集（如果1比2大，一定要是1中包含2所有元素，还有2没有的元素，那么1就是2的超集）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "d1bdc0cd",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "set1=set([1,2,5,8,0])\n",
    "set2=set([1,2,5,8,0,10,12])\n",
    "set3=set([1,2])\n",
    "\n",
    "print(set3.isdisjoint(set1))\n",
    "print(set1.issubset(set2))\n",
    "print(set2.issuperset(set1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf3e4914",
   "metadata": {},
   "source": [
    "* pop()用来删除集合内任意元素，remove()用来删除指定元素\n",
    "\n",
    "* clear()用来删除所有元素，并且将集合设置为空集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "6f33e7a1",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 5, 8, 10, 12}\n"
     ]
    }
   ],
   "source": [
    "set2=set([1,2,5,8,0,10,12])\n",
    "set2.pop()\n",
    "print(set2)#随机删除了元素0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "369c1095",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 5, 8, 10, 12}\n"
     ]
    }
   ],
   "source": [
    "set2=set([1,2,5,8,10,12])\n",
    "set2.remove(2)\n",
    "print(set2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "ea925287",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set()"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set2.clear()\n",
    "set2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32cd6c97",
   "metadata": {},
   "source": [
    "## 3.4 字符串\n",
    "\n",
    "### 3.4.1 内置函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee2a2266",
   "metadata": {},
   "source": [
    "* find函数可以找到相应字符串的位置\n",
    "* capitalize可以将第一个元素大写，upper是全部大写，lower则是全部小写,title则是将每个单词的首字母大写\n",
    "\n",
    "* center可以指定字符串的宽度，并且居中对齐\n",
    "* zfill可以将不够长度的地方填充0\n",
    "* expandtabs可以更改间距(在字符串中标注空格的情况下修改每个空格占地大小）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "7f97252f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-1\n",
      "CALIFORNIA DREAMING\n",
      "california dreaming\n",
      "California dreaming\n",
      "California Dreaming\n",
      "     California Dreaming      \n",
      "00000000000California Dreaming\n",
      "C  A L I F   O R N I A\n"
     ]
    }
   ],
   "source": [
    "string1=\"california dreaming\"\n",
    "print(string1.find(\"Dr\"))\n",
    "print(string1.upper())\n",
    "print(string1.lower())\n",
    "print(string1.capitalize())\n",
    "print(string1.title())\n",
    "\n",
    "string1=string1.title()\n",
    "print(string1.center(30))\n",
    "print(string1.zfill(30))\n",
    "string1=\"C\\tA L I F\\t O R N I A\"\n",
    "print(string1.expandtabs(3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43864453",
   "metadata": {},
   "source": [
    "* endswith检查是否以指定的字符结尾，返回一个T/F的值\n",
    "* count计算字符串中特定字符的数量\n",
    "\n",
    "* join可以在字符串中添加字符，也可以将列表转成字符串\n",
    "* 对应的split可以将字符串转化成列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ba0df816",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "3\n",
      "5axa6\n",
      "['C', 'a', 'l', 'i', 'f', 'o', 'r', 'n', 'i', 'a', ' ', 'D', 'r', 'e', 'a', 'm', 'i', 'n', 'g'] <class 'list'>\n",
      "California Dreaming <class 'str'>\n",
      "Dreaming\n"
     ]
    }
   ],
   "source": [
    "string1=\"california dreaming\".title()\n",
    "print(string1.endswith(\"g\"))#endswith检验\n",
    "print(string1.count(\"a\"))\n",
    "\n",
    "print(\"a\".join(\"5x6\"))#在每两个字符中间都加入a\n",
    "a=list(string1)\n",
    "print(a,type(a))\n",
    "b=\"\".join(a)#双引号里面是列表中每个元素之间相隔的东西，可以是空格也可以是字符\n",
    "print(b,type(b))\n",
    "c=\"\".join(a)[-8:]#也可以只将部分转成字符\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d0363d7d",
   "metadata": {},
   "source": [
    "* 默认以空格作为分隔符\n",
    "* 分割成2个\n",
    "* 分割成3个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "088ed7d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['California', 'Dreaming']\n",
      "['C', 'liforni', ' Dreaming']\n",
      "['C', 'liforni', ' Dre', 'ming']\n"
     ]
    }
   ],
   "source": [
    "print(b.split())\n",
    "print(b.split(\"a\",2))\n",
    "print(b.split(\"a\",3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6106531",
   "metadata": {},
   "source": [
    "* strip用来删除左右不需要的元素，默认删除空格，也可以删除特定字符\n",
    "* lstrip和rstrip分别只在左右实现strip的功能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "bb77d422",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "california\n",
      "california-- **\n",
      "  5* california\n"
     ]
    }
   ],
   "source": [
    "string=\"  5* california-- **\"\n",
    "print(string.strip(\" 5*-\"))#删除的是引号里包含的所有字符\n",
    "print(string.lstrip(\" 5*-\"))\n",
    "print(string.rstrip(\" 5*-\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "796076c1",
   "metadata": {},
   "source": [
    "### 3.4.2  词典及其函数\n",
    "\n",
    "首先定义一个词典,字典和列表不同的是他可以定义自己的索引名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "fca372bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'> <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "d0={}\n",
    "d1=dict()\n",
    "print(type(d0),type(d1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "471fc8a3",
   "metadata": {},
   "source": [
    "一般定义方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "5d3cc619",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'key0': 234, 'key1': [1, 4, 5], 'key2': 'california'}\n"
     ]
    }
   ],
   "source": [
    "dic={\"key0\":234,\"key1\":[1,4,5],\"key2\":\"california\"}\n",
    "print(dic)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c55f6e3",
   "metadata": {},
   "source": [
    "可以直接将两个列表合成一个词典（zip函数）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "44d33b82",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'zip'>\n",
      "{'One': 1, 'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5}\n",
      "{'One': 1, 'Two': 2, 'Three': 3, 'Five': 4}\n"
     ]
    }
   ],
   "source": [
    "names = ['One', 'Two', 'Three', 'Four','Five' ]\n",
    "numbers = [1, 2, 3, 4, 5]\n",
    "dic1=zip(names,numbers)\n",
    "print(type(dic1))#合并之后是zip类型的，打印必须转化成词典\n",
    "print(dict(dic1))\n",
    "\n",
    "#如果两个列表元素不等，按少的来，顺序对应\n",
    "names = ['One', 'Two', 'Three','Five' ]\n",
    "numbers = [1, 2, 3, 4, 5]\n",
    "dic2=zip(names,numbers)\n",
    "print(dict(dic2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30072334",
   "metadata": {},
   "source": [
    "**循环构建字典的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "7ca7e802",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'One': 1, 'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5}\n",
      "{'One': 1, 'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5}\n"
     ]
    }
   ],
   "source": [
    "names=['One', 'Two', 'Three', 'Four','Five' ]\n",
    "numbers=list(range(1,6))\n",
    "\n",
    "dic3=dict()\n",
    "for i in range(len(names)):\n",
    "    dic3[names[i]]=numbers[i]\n",
    "print(dic3)\n",
    "\n",
    "#Another approach\n",
    "dic4={names[i]:numbers[i] for i in range(len(names))}\n",
    "print(dic4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b657b2b",
   "metadata": {},
   "source": [
    "* values返回词典中所有的值\n",
    "* keys返回词典中所有的索引\n",
    "* item返回的是所有的索引和值，储存在了一个元组里"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "1aeefe7d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_values([1, 2, 3, 4, 5])\n",
      "dict_keys(['One', 'Two', 'Three', 'Four', 'Five'])\n",
      "dict_items([('One', 1), ('Two', 2), ('Three', 3), ('Four', 4), ('Five', 5)])\n",
      "<class 'dict_items'>\n"
     ]
    }
   ],
   "source": [
    "print(dic4.values())\n",
    "print(dic4.keys())\n",
    "\n",
    "#item的结果如下，里的每一个元素都是一个元组\n",
    "print(dic4.items())\n",
    "print(type(dic4.items()))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e4a0f91",
   "metadata": {},
   "source": [
    "pop用来删除指定元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "86f8ec24",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5}\n"
     ]
    }
   ],
   "source": [
    "dic4={'One': 1, 'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5}\n",
    "pop_word=dic4.pop('One')#pop框里的关键字是索引值\n",
    "print(dic4)"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "原始单元格格式",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
